package com.sneakxy.cloudbase.platform.bean.dto;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sneakxy.cloudbase.platform.bean.Menu;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 菜单信息DTO
 * @author 潜行的虚影
 * @see
 */
@Data
@ApiModel("菜单信息")
@EqualsAndHashCode
@JsonIgnoreProperties(ignoreUnknown = true)
public class MenuDTO implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5136885061255385691L;

	public final static String MENU_TYPE_SYSTEM = "MENU-TYPE-SYSTEM";

	@NotBlank(groups = {ValidEdit.class})
	@Size(max=32, groups = {ValidEdit.class})
	@ApiModelProperty("菜单ID")
	private String id;

	@Size(max=50, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("菜单图标")
	private String icon;

	@Size(max=100, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("菜单路由")
	private String router;

	@NotBlank(groups = {ValidAdd.class, ValidEdit.class})
	@Size(max=100, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("菜单名称")
	private String name;

	@Size(max=64, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("权限标志")
	private String permission;

	@NotNull(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("菜单排序")
	private Integer sort;

	@Size(max=50, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("国际化")
	private String i18n;

	@NotBlank(groups = ValidAdd.class)
	@Size(max=32, groups = ValidAdd.class)
	@ApiModelProperty("菜单类型")
	private String type;

	@NotNull(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("是否开启")
	private Boolean enable;

	@NotBlank(groups = ValidAdd.class)
	@Size(max=32, groups = ValidAdd.class)
	@ApiModelProperty("响应类型")
	private String menuType;

	@ApiModelProperty("创建时间")
	private String createtime;

	@ApiModelProperty("更新时间")
	private String updatetime;

	@ApiModelProperty("下级菜单")
	private List<MenuDTO> children = new ArrayList<>();

	public static MenuDTO from(Menu entity) {
		MenuDTO result = new MenuDTO();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<MenuDTO> from(List<Menu> list) {
		List<MenuDTO> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(from(entity));
		});
		return result;
	}
	
	public static Menu to(MenuDTO entity) {
		Menu result = new Menu();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<Menu> to(List<MenuDTO> list) {
		List<Menu> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(to(entity));
		});
		return result;
	}

}
